package com.woniu.rabbit.delay_model.producer;

import com.woniu.rabbit.delay_model.exchange.DelayExchangeConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class DelayProducerService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 创建生产者
     * */
    public void delayProducer(String msg){
        log.info("生产者发送消息：{}",msg);
        // 设置消息过期时间 30s

        // 发送消息并设置消息 延迟时间
        rabbitTemplate.convertAndSend(
                DelayExchangeConfig.DELAYX_EXCHANGE,
                DelayExchangeConfig.DELAYX_ROUTING_KEY,
                msg,
                message -> {    // 设置 延迟消费时间
            message.getMessageProperties().setHeader("x-delay", 30000);
                    return message;
                });
        log.info("发送延迟消息：{}，延迟时间：{} ms", msg, 30000);
    }
}
